package com.client.clovece;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;


public class GamePanel extends JPanel{
	private Dimension d;
	private LocalGame parent;
	ArrayList <TileButton> grid;
	ArrayList <TileButton> p1start;
	ArrayList <TileButton> p2start;
	ArrayList <TileButton> p3start;
	ArrayList <TileButton> p4start;
	
	ArrayList <TileButton> p1end;
	ArrayList <TileButton> p2end;
	ArrayList <TileButton> p3end;
	ArrayList <TileButton> p4end;
	
	ArrayList <TileButton> game;
	ArrayList <TileButton> crossroads;
	
	public GamePanel(LocalGame parent){
		this.parent = parent;
		d = new Dimension(520,520);
		GridLayout l = new GridLayout(13,13,0,0);
		setLayout(l);
		setPreferredSize(d);
		setMinimumSize(d);
		setMaximumSize(d);
		grid = new ArrayList<TileButton>();
		p1start = new ArrayList<TileButton>();
		p2start = new ArrayList<TileButton>();
		p3start = new ArrayList<TileButton>();
		p4start = new ArrayList<TileButton>();
		p1end = new ArrayList<TileButton>();
		p2end = new ArrayList<TileButton>();
		p3end = new ArrayList<TileButton>();
		p4end = new ArrayList<TileButton>();
		game = new ArrayList<TileButton>();
		crossroads = new ArrayList<TileButton>();
		initGrid();
		setGrid();
		setStartTiles();
		setEndTiles();
		setGameTiles();
		setGameTileIds();
		setCrossroads();
	}
	
	private void initGrid(){
		for(int i=0; i<169; i++){
			//System.out.println(i+"\n");
			grid.add(new TileButton());
		}
	}
	
	private void setGrid(){
		TileButton t;
		for(int i=0; i<169; i++){
			t = grid.get(i);
			t.setDefaultState(ButtonStates.NONPLAYABLE);
			t.setIcon(ButtonStates.NONPLAYABLE);
			t.setTempState(ButtonStates.EMPTY);
			t.addMouseListener(parent);
			t.setGridArrId(i);
			this.add(grid.get(i));
		}
	}
	
	private void setStartTiles(){
		TileButton t;
		//pl1
		t = grid.get(0); 
		t.setDefaultState(ButtonStates.PL1START);
		t.setTempStateAndIcon(ButtonStates.PL1BRIGHT);
		p1start.add(t);
		//----------------------------
		t = grid.get(1);
		t.setDefaultState(ButtonStates.PL1START);
		t.setTempStateAndIcon(ButtonStates.PL1BRIGHT);
		p1start.add(t);
		//----------------------------
		t = grid.get(13);
		t.setDefaultState(ButtonStates.PL1START);
		t.setTempStateAndIcon(ButtonStates.PL1BRIGHT);
		p1start.add(t);
		//----------------------------
		t = grid.get(14);
		t.setDefaultState(ButtonStates.PL1START);
		t.setTempStateAndIcon(ButtonStates.PL1BRIGHT);
		p1start.add(t);
		//pl2
		t = grid.get(11);
		t.setDefaultState(ButtonStates.PL2START);
		t.setTempStateAndIcon(ButtonStates.PL2BRIGHT);
		p2start.add(t);
		//----------------------------
		t = grid.get(12);
		t.setDefaultState(ButtonStates.PL2START);
		t.setTempStateAndIcon(ButtonStates.PL2BRIGHT);
		p2start.add(t);
		//----------------------------
		t = grid.get(24);
		t.setDefaultState(ButtonStates.PL2START);
		t.setTempStateAndIcon(ButtonStates.PL2BRIGHT);
		p2start.add(t);
		//----------------------------
		t = grid.get(25);
		t.setDefaultState(ButtonStates.PL2START);
		t.setTempStateAndIcon(ButtonStates.PL2BRIGHT);
		p2start.add(t);
		//pl3
		t = grid.get(154);
		t.setDefaultState(ButtonStates.PL3START);
		t.setTempStateAndIcon(ButtonStates.PL3BRIGHT);
		p3start.add(t);
		//-----------------------------
		t = grid.get(155);
		t.setDefaultState(ButtonStates.PL3START);
		t.setTempStateAndIcon(ButtonStates.PL3BRIGHT);
		p3start.add(t);
		//-----------------------------
		t = grid.get(167);
		t.setDefaultState(ButtonStates.PL3START);
		t.setTempStateAndIcon(ButtonStates.PL3BRIGHT);
		p3start.add(t);
		//------------------------------
		t = grid.get(168);
		t.setDefaultState(ButtonStates.PL3START);
		t.setTempStateAndIcon(ButtonStates.PL3BRIGHT);
		p3start.add(t);
		//pl4
		t = grid.get(143);
		t.setDefaultState(ButtonStates.PL4START);
		t.setTempStateAndIcon(ButtonStates.PL4BRIGHT);
		p4start.add(t);
		//------------------------------
		t = grid.get(144);
		t.setDefaultState(ButtonStates.PL4START);
		t.setTempStateAndIcon(ButtonStates.PL4BRIGHT);
		p4start.add(t);
		//------------------------------
		t = grid.get(156);
		t.setDefaultState(ButtonStates.PL4START);
		t.setTempStateAndIcon(ButtonStates.PL4BRIGHT);
		p4start.add(t);
		//-------------------------------
		t = grid.get(157);
		t.setDefaultState(ButtonStates.PL4START);
		t.setTempStateAndIcon(ButtonStates.PL4BRIGHT);
		p4start.add(t);
	}
	//konzistence nazvu promennych player, role, state
	public ArrayList<TileButton> getStartTiles(ButtonStates playerID){
		
		if(playerID.equals(ButtonStates.PL1BRIGHT) || playerID.equals(ButtonStates.PL1DIM)) return p1start;
		else if(playerID.equals(ButtonStates.PL2BRIGHT) || playerID.equals(ButtonStates.PL2DIM)) return p2start;
		else if(playerID.equals(ButtonStates.PL3BRIGHT) || playerID.equals(ButtonStates.PL3DIM)) return p3start;
		else if(playerID.equals(ButtonStates.PL4BRIGHT) || playerID.equals(ButtonStates.PL4DIM)) return p4start;
		else return null;
		
	}
	
	public TileButton getCrossroad(ButtonStates playerID){
		if(playerID.equals(ButtonStates.PL1BRIGHT) || playerID.equals(ButtonStates.PL1DIM)) return crossroads.get(ButtonStates.PL1START.ordinal());
		else if(playerID.equals(ButtonStates.PL2BRIGHT) || playerID.equals(ButtonStates.PL2DIM)) return crossroads.get(ButtonStates.PL2START.ordinal());
		else if(playerID.equals(ButtonStates.PL3BRIGHT) || playerID.equals(ButtonStates.PL3DIM)) return crossroads.get(ButtonStates.PL3START.ordinal());
		else if(playerID.equals(ButtonStates.PL4BRIGHT) || playerID.equals(ButtonStates.PL4DIM)) return crossroads.get(ButtonStates.PL4START.ordinal());
		else return null;
	}
	
	private void setEndTiles(){
		//pl1
		sptDown(19, 4, ButtonStates.PL1END);
		//pl2
		sptLeft(89, 4, ButtonStates.PL2END);
		//pl3
		sptUp(149, 4, ButtonStates.PL3END);
		//pl4
		sptRight(79, 4, ButtonStates.PL4END);
		
	}
	
	public ArrayList<TileButton> getEndTiles(ButtonStates role){
		if(role.equals(ButtonStates.PL1END)) return p1end;
		else if(role.equals(ButtonStates.PL2END)) return p2end;
		else if(role.equals(ButtonStates.PL3END)) return p3end;
		else if(role.equals(ButtonStates.PL4END)) return p4end;
		else return null;
	}
	
	private void setCrossroads(){
		crossroads.add(grid.get(6));
		crossroads.add(grid.get(90));
		crossroads.add(grid.get(162));
		crossroads.add(grid.get(78));
		crossroads.get(0).setDefaultState(ButtonStates.PL1CROSSROAD);
		crossroads.get(1).setDefaultState(ButtonStates.PL2CROSSROAD);
		crossroads.get(2).setDefaultState(ButtonStates.PL3CROSSROAD);
		crossroads.get(3).setDefaultState(ButtonStates.PL4CROSSROAD);
		//crossroads.get(0).setOccupant(TileButton.PL1);
		//crossroads.get(1).setOccupant(TileButton.PL2);
		//crossroads.get(2).setOccupant(TileButton.PL3);
		//crossroads.get(3).setOccupant(TileButton.PL4);
		//mozna to pude bez nich... odectem ujite vzdalenosti
		//kazda figurka od zacatku po krizovatku ujde 49 kroku
		//78,90,162
	}
	
	//nakresli policka, po kterych se pohybuji hraci
	private void setGameTiles(){
		sptRight(6, 3, ButtonStates.GAME);
		sptDown(21, 4, ButtonStates.GAME);
		sptRight(61, 4,ButtonStates.GAME);
		sptDown(77, 4, ButtonStates.GAME);
		sptLeft(115, 4, ButtonStates.GAME);
		sptDown(125, 4, ButtonStates.GAME);
		sptLeft(163, 4, ButtonStates.GAME);
		sptUp(147, 4, ButtonStates.GAME);
		sptLeft(107, 4, ButtonStates.GAME);
		sptUp(91, 4, ButtonStates.GAME);
		sptRight(53, 4, ButtonStates.GAME);
		sptUp(43, 4, ButtonStates.GAME);
		sptRight(5, 1, ButtonStates.GAME);
	}
	
	private void setGameTileIds(){
		for(int i=0; i<game.size(); i++){
			game.get(i).setRoleArrId(i);
		}
	}
	
	//init a line of buttons of one kind into their default state
	//posledni z rady policek nenastavi nasledovnika
	//nastavi ho az navazujici rada(jeji prvni prvek)
	private void sptRight(int start, int count, ButtonStates defaultState){
		TileButton t;
		for(int j=0; j<count; j++){
			t = grid.get(j+start);
			t.setDefaultState(defaultState);
			t.setIcon(defaultState);
			//t.setTempState(defaultState); !TODO remove
			
			if(defaultState.equals(ButtonStates.PL1START))	p1start.add(t);
			else if(defaultState.equals(ButtonStates.PL2START))	p2start.add(t);
			else if(defaultState.equals(ButtonStates.PL3START))p3start.add(t);
			else if(defaultState.equals(ButtonStates.PL4START))	p4start.add(t);
			
			else if(defaultState.equals(ButtonStates.PL1END)) p1end.add(t);
			else if(defaultState.equals(ButtonStates.PL2END)) p2end.add(t);
			else if(defaultState.equals(ButtonStates.PL3END)) p3end.add(t);
			else if(defaultState.equals(ButtonStates.PL4END)) p4end.add(t);
			
			else if(defaultState.equals(ButtonStates.GAME)) game.add(t);
		}
	}
	
	private void sptLeft(int start, int count, ButtonStates defaultState){
		TileButton t;
		for(int j=0; j<count; j++){
			t = grid.get(start-j);
			t.setDefaultState(defaultState);
			t.setIcon(defaultState);
			//t.setTempState(defaultState); !TODO remove
			
			if(defaultState.equals(ButtonStates.PL1START))	p1start.add(t);
			else if(defaultState.equals(ButtonStates.PL2START))	p2start.add(t);
			else if(defaultState.equals(ButtonStates.PL3START))p3start.add(t);
			else if(defaultState.equals(ButtonStates.PL4START))	p4start.add(t);
			
			else if(defaultState.equals(ButtonStates.PL1END)) p1end.add(t);
			else if(defaultState.equals(ButtonStates.PL2END)) p2end.add(t);
			else if(defaultState.equals(ButtonStates.PL3END)) p3end.add(t);
			else if(defaultState.equals(ButtonStates.PL4END)) p4end.add(t);
			
			else if(defaultState.equals(ButtonStates.GAME)) game.add(t);
		}
	}
	
	private void sptDown(int start, int count, ButtonStates defaultState){
		TileButton t;
		for(int j=0; j<count; j++){
			t = grid.get((j*13)+start);//explain
			t.setDefaultState(defaultState);
			t.setIcon(defaultState);
			//t.setTempState(defaultState); !TODO remove
			
			if(defaultState.equals(ButtonStates.PL1START))	p1start.add(t);
			else if(defaultState.equals(ButtonStates.PL2START))	p2start.add(t);
			else if(defaultState.equals(ButtonStates.PL3START))p3start.add(t);
			else if(defaultState.equals(ButtonStates.PL4START))	p4start.add(t);
			
			else if(defaultState.equals(ButtonStates.PL1END)) p1end.add(t);
			else if(defaultState.equals(ButtonStates.PL2END)) p2end.add(t);
			else if(defaultState.equals(ButtonStates.PL3END)) p3end.add(t);
			else if(defaultState.equals(ButtonStates.PL4END)) p4end.add(t);
			
			else if(defaultState.equals(ButtonStates.GAME)) game.add(t);
				
		}
	}
	
	private void sptUp(int start, int count, ButtonStates defaultState){
		TileButton t;
		for(int j=0; j<count; j++){
			t = grid.get(start-(j*13));
			t.setDefaultState(defaultState);
			t.setIcon(defaultState);
			//t.setTempState(defaultState); !TODO remove
			
			if(defaultState.equals(ButtonStates.PL1START))	p1start.add(t);
			else if(defaultState.equals(ButtonStates.PL2START))	p2start.add(t);
			else if(defaultState.equals(ButtonStates.PL3START))p3start.add(t);
			else if(defaultState.equals(ButtonStates.PL4START))	p4start.add(t);
			
			else if(defaultState.equals(ButtonStates.PL1END)) p1end.add(t);
			else if(defaultState.equals(ButtonStates.PL2END)) p2end.add(t);
			else if(defaultState.equals(ButtonStates.PL3END)) p3end.add(t);
			else if(defaultState.equals(ButtonStates.PL4END)) p4end.add(t);
			
			else if(defaultState.equals(ButtonStates.GAME)) game.add(t);
				
		}
	}

	
	
}

